package my.test.flyweight.source;

import java.util.HashMap;

/**
 * @Author 许玉康
 * @Date 2019-11-4 12:29
 */
public class FlyweightFactory {

    //定义一个池容器
    private static HashMap<String, Flyweight> pool = new HashMap<String, Flyweight>();
    //享元工厂
    public static Flyweight getFlyweight(String extrinsic) {
        //需要返回的对象
        Flyweight flyweight = null;
        //在池中没有该对象
        if(pool.containsKey(extrinsic)) {
            flyweight = new ConcreteFlyweight1(extrinsic);
        } else {
            //根据外部状态创建享元对象
            flyweight = new ConcreteFlyweight1(extrinsic);
            pool.put(extrinsic, flyweight);
        }
        return flyweight;
    }
}
